import {h} from 'vue';
import {FormItemRule, NTag} from 'naive-ui';
import {FormSchema} from '@/components/Form';


export const columns = [
  {
    type: 'selection',
    key: 'selection'
  },

  {
    title: '用户ID',
    key: 'id',
  },
  {
    title: '用户名',
    key: 'username',
  },
  {
    title: '角色',
    key: 'roleType',
    render: (row) => {
      switch(row.roleType){
        case "admin":{
          return "系统管理员"
        }
        case "super":{
          return "超级管理员"
        }
        case "staff":{
          return "运维人员"
        }
        case "customer":{
          return "客服维护员"
        }
      }
    }
  },
  {
    title: '手机号码',
    key: 'phone',
  },
  {
    title: '状态',
    key: 'status',
    render(row) {
      return h(
        NTag,
        {
          type: row.status == 'able' ? 'success' : 'error',
        },
        {
          default: () => (row.status == 'able' ? '启用' : '禁用'),
        }
      );
    },
  },
  {
    title: "用户描述",
    key: "desc"
  },
  {
    title: "创建时间",
    key: "createTime"
  }
];
let password = '';

let staff = false;

export function setIsStaff(isStaff) {
  staff = isStaff;
  console.log(isStaff);
}

export const addCloumns: FormSchema[] = [
  {
    field: 'username',
    component: 'NInput',
    label: '用户名称',
    labelMessage: '提示',
    giProps: {
      span: 1,
    },
    componentProps: {
      placeholder: '请输入用户名称',
    },
    rules: [{required: true, message: '请输入用户名称', trigger: ['blur']}],
  },
  {
    field: 'password',
    component: 'NInput',
    label: '密码',
    componentProps: {
      type: "password",
      placeholder: "密码",
      onInput: (e: any) => {
        password = e;
      },
    },
    rules: [{required: true, message: '密码', trigger: ['blur']},],
  },
  {
    field: 'repassword',
    component: 'NInput',
    label: '再次输入密码',
    componentProps: {
      type: "password",
      placeholder: "密码"
    },
    rules: [{required: true, message: '再次输入密码', trigger: ['blur']}, {
      validator: validatorPassword,
      message: '两次密码输入不一致',
      trigger: ['blur', 'password-input']
    }],
  },
  {
    field: 'phone',
    component: 'NInput',
    label: '电话号码',
    componentProps: {
      type: "input",
      placeholder: "电话号码",
    },
    rules: [{
      required: true,
      message: "电话号码格式错误",
      trigger: ['input'],
      validator: (rule: FormItemRule, value: string) => {
        const regPhone: RegExp = /^1[3-9]\d{9}$/;
        return regPhone.test(value)
      }
    }
    ],
  }, {
    field: 'roleType',
    label: '角色类型',
    slot: 'roleSlot',
  },
  {
    field: 'chanList',
    label: '通道管理',
    hidden: "true",
    labelMessage: "只有运维用户需要选择通道，运维用户必须选择",
    slot: 'chanSlot',
    rules: [{
      message: "运维用户必须选择通道信息",
      trigger: ["blur"],
      validator: (rule: FormItemRule, value: string) => {
        if (staff) {
          return value != ''
        }
        return true
      }
    }]
  },
  {
    field: 'status',
    component: 'NSwitch',
    label: '状态',
    defaultValue: true
  },
  {
    field: 'desc',
    component: 'NInput',
    label: '备注',
    componentProps: {
      placeholder: "备注",
      type: 'textarea'
    },
  }
];

function validatorPassword(rule: FormItemRule, value: string) {
  return value === password
}

export const editCloumns: FormSchema[] = [
  {
    field: 'username',
    component: 'NInput',
    label: '用户名称',
    labelMessage: '提示',
    giProps: {
      span: 1,
    },
    componentProps: {
      placeholder: '请输入用户名称',
    },
    rules: [{required: true, message: '请输入用户名称', trigger: ['blur']}],
  },
  {
    field: 'phone',
    component: 'NInput',
    label: '电话号码',
    componentProps: {
      type: "input",
      placeholder: "电话号码",
    },
    rules: [{
      required: true,
      message: "电话号码格式错误",
      trigger: ['input'],
      validator: (rule: FormItemRule, value: string) => {
        const regPhone: RegExp = /^1[3-9]\d{9}$/;
        return regPhone.test(value)
      }
    }
    ],
  }, {
    field: 'roleType',
    label: '角色类型',
    slot: 'roleSlot'
  },
  {
    field: 'chanList',
    label: '通道选择',
    slot: 'chanSlot'
  },
  {
    field: 'status',
    component: 'NSwitch',
    label: '状态',
    defaultValue: true
  },
  {
    field: 'desc',
    component: 'NInput',
    label: '备注',
    componentProps: {
      placeholder: "备注",
      type: 'textarea'
    },
  }

];
